<?php

namespace App\Services;

use App\Models\AdminRole;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;

class AdminRoleService extends BaseService
{
    /**
     * 创建角色
     * @param $data
     * @return bool
     */
    public function create($data): bool
    {
        try {
            AdminRole::query()->create($data);
            return true;
        } catch (\Exception $exception) {
            return false;
        }
    }

    /**
     * 根据ID修改角色
     * @param $data
     * @param $id
     * @return bool
     */
    public function updateById($data,$id): bool
    {
        try {
            $result = AdminRole::query()->where('id','=',$id)->update($data);
            if ($result > 0) {
                return true;
            }
            return false;
        }catch (\Exception $exception) {
            return false;
        }
    }

    /**
     * 根据ID删除角色
     * @param $id
     * @return bool
     */
    public function deleteById($id): bool
    {
        try {
            $result = AdminRole::query()->where('id','=',$id)->delete();
            if ($result > 0) {
                return true;
            }
            return false;
        } catch (\Exception $exception) {
            return false;
        }
    }

    /**
     * 根据ID批量删除角色
     * @param array $ids
     * @return bool
     */
    public function deleteBatchByIds(array $ids): bool
    {
        if (empty($ids)) {
            return false;
        }
        try {
            $result = AdminRole::query()->whereIn('id',$ids)->delete();
            if ($result > 0) {
                return true;
            }
            return false;
        }catch (\Exception $exception){
            return false;
        }
    }

    /**
     * 获取所有角色
     * @return array
     */
    public function listAll(): array
    {
        $result = AdminRole::query()->get();
        if ($result->count() >0) {
            return $result->toArray();
        }
        return [];
    }

    /**
     * 分页获取角色列表
     * @param $keyword
     * @param $pageSize
     * @param $pageNum
     * @return LengthAwarePaginator
     */
    public function list($keyword,$pageSize,$pageNum): LengthAwarePaginator
    {
        $query = AdminRole::query();
        if ($keyword) {
            $query = $query->where('name','like','%'.$keyword.'%');
        }
        return $query->paginate($pageSize, ['*'], $pageName = 'page', $pageNum);
    }

    /**
     * 修改角色状态
     * @param $status
     * @param $id
     * @return bool
     */
    public function updateStatusById($status,$id): bool
    {
        try {
            $result = AdminRole::query()->where('id','=',$id)->update(['status' => $status]);
            if ($result > 0) {
                return true;
            }
            return false;
        } catch (\Exception $exception) {
            return false;
        }
    }

    /**
     * 根据角色ID查找菜单信息
     * @param $roleId
     * @return array
     */
    public function getMenuById($roleId): array
    {
        $result = AdminRole::query()->where('id','=',$roleId)->with(['menus'])->first();
        if (is_null($result)) {
            return [];
        }
        return $result->toArray();
    }

    /**
     * 根据角色IDs查找菜单信息
     * @param $roleIds
     * @return array
     */
    public function getMenuByIds($roleIds): array
    {
        $result = AdminRole::query()->whereIn('id',$roleIds)->with(['menus'])->first();
        if (is_null($result)) {
            return [];
        }
        return $result->toArray();
    }

    /**
     * 根据角色ID查找资源信息
     * @param $roleId
     * @return array
     */
    public function getResourceById($roleId): array
    {
        $result = AdminRole::query()->where('id','=',$roleId)->with(['resources'])->first();
        if (is_null($result)) {
            return [];
        }
        return $result->toArray();
    }

    /**
     * 给角色分配菜单
     * @param $roleId
     * @param $menuIds
     * @return bool
     */
    public function allocMenu($roleId,$menuIds): bool
    {
        if (empty($menuIds)|| empty($roleId)) {
            return false;
        }
        $roleInfo = AdminRole::query()->where('id','=',$roleId)->with(['menus'])->first();
        if ($roleInfo->attach($menuIds)) {
            return true;
        }
        return false;
    }

    /**
     * 给角色分配菜单
     * @param $roleId
     * @param $resourceIds
     * @return bool
     */
    public function allocResource($roleId,$resourceIds): bool
    {
        if (empty($resourceIds)|| empty($roleId)) {
            return false;
        }
        $roleInfo = AdminRole::query()->where('id','=',$roleId)->with(['resources'])->first();
        if ($roleInfo->attach($resourceIds)) {
            return true;
        }
        return false;
    }

}
